﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YJKArchUtils.Utils;
using YJKInspectModel.ModelValidation.Commands;

namespace YJKInspectModel.ModelValidation.StairsRamp
{
	public partial class StairsRampValidationResult : System.Windows.Forms.Form
	{
		public StairsRampValidationResult(ExternalCommandData cmdData, List<List<StairsResult>> listStairsGroups, List<List<RampResult>> listRampGroups)
		{
			this.m_UIDoc = cmdData.Application.ActiveUIDocument;
			this.m_Doc = this.m_UIDoc.Document;
			this.m_listStairsGroups = listStairsGroups;
			this.m_listRampGroups = listRampGroups;
			this.InitializeComponent();
		}

		private void StairsRampValidationResult_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.InitialTreeElement();
			this.lblStairsCount.Text = this.GetStairsCount(this.m_listStairsGroups).ToString();
			this.lblRampCount.Text = this.GetRampCount(this.m_listRampGroups).ToString();
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		public void PressEsc()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			StairsRampValidationResult.SendMessage(CmdStairsRampValidation._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			StairsRampValidationResult.keybd_event((byte)bVk, 0, dwFlags, 0);
			StairsRampValidationResult.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		protected override bool ProcessKeyEventArgs(ref Message m)
		{
			int num = 256;
			int num2 = 27;
			if (m.Msg == num && m.WParam.ToInt32() == num2)
			{
				base.Close();
			}
			return base.ProcessKeyEventArgs(ref m);
		}

		public void ResetContinueBreak()
		{
			this.IfBreak = true;
			this.IfManualEsc = true;
		}

		private void InitialTreeElement()
		{
			this.treeView.Nodes.Clear();
			int num = 1;
			foreach (List<StairsResult> list in this.m_listStairsGroups)
			{
				TreeNode treeNode = this.treeView.Nodes.Add("楼梯组" + num.ToString());
				foreach (StairsResult stairsResult in list)
				{
					treeNode.Nodes.Add(stairsResult.m_idStairs.ToString());
				}
				num++;
			}
			num = 1;
			foreach (List<RampResult> list2 in this.m_listRampGroups)
			{
				TreeNode treeNode = this.treeView.Nodes.Add("坡道组" + num.ToString());
				foreach (RampResult rampResult in list2)
				{
					treeNode.Nodes.Add(rampResult.m_idRamp.ToString());
				}
				num++;
			}
		}

		private List<string> GetWarnInfo(string sNodeName)
		{
			List<string> result = new List<string>();
			foreach (List<StairsResult> list in this.m_listStairsGroups)
			{
				foreach (StairsResult stairsResult in list)
				{
					if (stairsResult.m_idStairs.ToString().Contains(sNodeName))
					{
						this.m_bIsStairs = true;
						if (stairsResult.m_idLinkInstance == null)
						{
							this.m_CurrentId = stairsResult.m_idStairs;
						}
						else
						{
							this.m_CurrentId = stairsResult.m_idLinkInstance;
						}
						result = stairsResult.m_listWarnInfo;
						break;
					}
				}
			}
			foreach (List<RampResult> list2 in this.m_listRampGroups)
			{
				foreach (RampResult rampResult in list2)
				{
					if (rampResult.m_idRamp.ToString().Contains(sNodeName))
					{
						this.m_bIsStairs = false;
						this.m_CurrentId = rampResult.m_idRamp;
						result = rampResult.m_listWarnInfo;
						break;
					}
				}
			}
			return result;
		}

		private int GetStairsCount(List<List<StairsResult>> listStairsGroups)
		{
			int num = 0;
			foreach (List<StairsResult> source in listStairsGroups)
			{
				num += source.Count<StairsResult>();
			}
			return num;
		}

		private int GetRampCount(List<List<RampResult>> listRampGroups)
		{
			int num = 0;
			foreach (List<RampResult> source in listRampGroups)
			{
				num += source.Count<RampResult>();
			}
			return num;
		}

		private void GetFloorListByLevels(List<Level> floorPlanList, ref List<ViewPlan> floorInfo)
		{
			List<ViewPlan> list = new List<ViewPlan>();
			foreach (Element element in new FilteredElementCollector(this.m_UIDoc.Document).OfClass(typeof(ViewPlan)))
			{
				ViewPlan viewPlan = element as ViewPlan;
				if (viewPlan != null && (int)viewPlan.ViewType == 1 && viewPlan.GenLevel != null)
				{
					list.Add(viewPlan);
				}
			}
			using (List<Level>.Enumerator enumerator2 = floorPlanList.GetEnumerator())
			{
				while (enumerator2.MoveNext())
				{
					Level level = enumerator2.Current;
					int num = list.FindIndex((ViewPlan item) => Geometry.IsEqual(item.GenLevel.Elevation, level.Elevation));
					if (num != -1)
					{
						floorInfo.Add(list[num]);
					}
				}
			}
		}

		private void SwitchStairsFloorView(ElementId currentId)
		{
			if (null == currentId)
			{
				return;
			}
			Element element = this.m_Doc.GetElement(this.m_CurrentId);
			double num;
			if (element is Stairs)
			{
				num = (element as Stairs).BaseElevation;
			}
			else
			{
				if (!(element is RevitLinkInstance))
				{
					return;
				}
				num = (element as RevitLinkInstance).GetTransform().Origin.Z;
			}
			List<Level> sortLevels = StairsRampValidationManager.GetSortLevels(this.m_Doc, double.MinValue, double.MaxValue);
			List<ViewPlan> list = new List<ViewPlan>();
			this.GetFloorListByLevels(sortLevels, ref list);
			int num2 = 0;
			using (List<ViewPlan>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.GenLevel.Elevation >= num)
					{
						break;
					}
					num2++;
				}
			}
			if (num2 == list.Count)
			{
				num2--;
			}
			if (this.m_UIDoc.ActiveView.Id.IntegerValue != list[num2].Id.IntegerValue)
			{
				this.m_UIDoc.ActiveView = list[num2];
			}
		}

		private void SwitchRampFloorView(ElementId currentId)
		{
			if (null == currentId)
			{
				return;
			}
			List<Level> sortLevels = StairsRampValidationManager.GetSortLevels(this.m_Doc, double.MinValue, double.MaxValue);
			List<ViewPlan> list = new List<ViewPlan>();
			this.GetFloorListByLevels(sortLevels, ref list);
			Group group = this.m_Doc.GetElement(this.m_CurrentId) as Group;
			int num = 0;
			using (List<ViewPlan>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.GenLevel.Id == group.LevelId)
					{
						break;
					}
					num++;
				}
			}
			if (this.m_UIDoc.ActiveView.Id.IntegerValue != list[num].Id.IntegerValue)
			{
				this.m_UIDoc.ActiveView = list[num];
			}
		}

		private void CentreHighlight()
		{
			foreach (UIView uiview in this.m_UIDoc.GetOpenUIViews())
			{
				Document document = this.m_UIDoc.Document;
				if (uiview.ViewId.ToString() == document.ActiveView.Id.ToString())
				{
					BoundingBoxXYZ boundingBoxXYZ = document.GetElement(this.m_CurrentId).get_BoundingBox(document.ActiveView);
					XYZ xyz = (boundingBoxXYZ.Max + boundingBoxXYZ.Min) / 2.0;
					IList<XYZ> zoomCorners = uiview.GetZoomCorners();
					XYZ xyz2 = (zoomCorners.ElementAt(0) + zoomCorners.ElementAt(1)) / 2.0;
					XYZ xyz3 = new XYZ(xyz2.X - xyz.X, xyz2.Y - xyz.Y, 0.0);
					uiview.ZoomAndCenterRectangle(zoomCorners.ElementAt(0) - xyz3, zoomCorners.ElementAt(1) - xyz3);
					RevitVersionFuncs.ClearSelection(this.m_UIDoc.Selection);
					List<Element> list = new List<Element>();
					list.Add(this.m_UIDoc.Document.GetElement(this.m_CurrentId));
					RevitVersionFuncs.SetSelection(this.m_UIDoc.Selection, list);
					this.m_UIDoc.RefreshActiveView();
				}
			}
		}

		private void lstView_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.ChangeActiveViewAndCenterHighLight();
		}

		private void ChangeActiveViewAndCenterHighLight()
		{
			if (null != this.m_CurrentId)
			{
				if (this.m_bIsStairs)
				{
					this.SwitchStairsFloorView(this.m_CurrentId);
				}
				else
				{
					this.SwitchRampFloorView(this.m_CurrentId);
				}
				this.CentreHighlight();
			}
		}

		private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
		{
			this.m_treNodeCur = this.treeView.SelectedNode;
			this.lstView.Clear();
			this.lstView.Columns.Add("规范", this.lstView.Size.Width);
			this.lstView.BeginUpdate();
			foreach (string text in this.GetWarnInfo(this.m_treNodeCur.Text))
			{
				ListViewItem listViewItem = new ListViewItem();
				if (text != null)
				{
					listViewItem.Text = text;
					this.lstView.Items.Add(listViewItem);
				}
			}
			this.lstView.EndUpdate();
			this.ChangeActiveViewAndCenterHighLight();
		}

		private void StairsRampValidationResult_SizeChanged(object sender, EventArgs e)
		{
			int width = base.Size.Width;
			int x = this.lstView.Location.X;
			int width2 = width - x;
			this.lstView.Size = new Size(width2, this.lstView.Size.Height);
			if (0 < this.lstView.Columns.Count)
			{
				this.lstView.Columns[0].Width = width2;
			}
			base.Size = new Size(width, 387);
		}

		private void StairsRampValidationResult_ResizeBegin(object sender, EventArgs e)
		{
			int width = base.Size.Width;
			base.Size = new Size(width, 387);
		}

		private void StairsRampValidationResult_ResizeEnd(object sender, EventArgs e)
		{
			int width = base.Size.Width;
			base.Size = new Size(width, 387);
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.PressEsc();
			base.Close();
		}

		private void StairsRampValidationResult_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.PressEsc();
		}

		private TreeNode m_treNodeCur;

		private List<List<StairsResult>> m_listStairsGroups;

		private List<List<RampResult>> m_listRampGroups;

		private ElementId m_CurrentId;

		private UIDocument m_UIDoc;

		private Document m_Doc;

		private bool m_bIsStairs = true;
	}
}
